package com.android.readcard.utils;

import android.app.Activity;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import com.android.readcard.R;
import com.kaeridcard.client.IClientCallBack;
import com.kaeridcard.client.IdCardItem;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import static com.kaeridcard.client.BtReaderClient.is_bt_connect;

/**
 * 拷贝授权文件
 */
public class GlobReadCardUtils {
    public static void copyLibFile(Activity mContext) {
        String path = Environment.getExternalStorageDirectory() + "/wltlib";
        String filename1 = path + "/" + "base.dat";
        String filename2 = path + "/" + "license.lic";
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdir();
        }
        try {
            if (!(new File(filename1)).exists()) {
                InputStream is = null;
                is = mContext.getApplicationContext().getResources().openRawResource(R.raw.base);
                int size = is.available();
                FileOutputStream fos = new FileOutputStream(filename1);
                byte[] buffer = new byte[8192];
                int count = 0;
                while ((count = is.read(buffer)) > 0) {
                    fos.write(buffer, 0, count);
                }
                fos.flush();
                fos.close();
                is.close();
            } else {
                Log.e("ComShell", "file exist:" + filename1);
            }
            if (!(new File(filename2)).exists()) {
                InputStream is = null;
                is = mContext.getApplicationContext().getResources().openRawResource(R.raw.license);
                int size = is.available();
                FileOutputStream fos = new FileOutputStream(filename2);
                byte[] buffer = new byte[8192];
                int count = 0;
                while ((count = is.read(buffer)) > 0) {
                    fos.write(buffer, 0, count);
                }
                fos.flush();
                fos.close();
                is.close();
            } else {
                Log.e("ComShell", "file exist:" + filename2);
            }
        } catch (FileNotFoundException e) {
            Log.e("Database", "File not found");
            e.printStackTrace();
        } catch (IOException e) {
            Log.e("Database", "IO exception");
            e.printStackTrace();
        }
    }

    private static int rssi_value;

    public static IClientCallBack mCallback = new IClientCallBack() {

        @Override
        public void onBtState(boolean is_connect) {
            is_bt_connect = is_connect;
        }

        private Handler btHandler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case 1:
                        if (!is_bt_connect)
                            break;
                        String dis_text;
                        int rssi = rssi_value & 0xff;

                        if ((rssi & 0x80) != 0) {
                            rssi = 0x100 - rssi;
                            dis_text = " -" + rssi;
                        } else if (rssi > 0) {
                            dis_text = " " + rssi;
                        } else
                            dis_text = " 0";
                        String connect_str = "蓝牙已连接到:" + "\n" +
                                "信号强度" + dis_text;
                        break;
                    default:
                        break;
                }
            }
        };

        @Override
        public void onIddataHandle(IdCardItem idCardItem) {
        }
    };
    /**
     * 卡尔KT8003 账号密码  加密方法
     */
    public static String getMD5(byte[] source) {
        String s = null;
        char hexDigits[] = { // 用来将字节转换成 16 进制表示的字符
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
                'e', 'f'};
        try {
            java.security.MessageDigest md = java.security.MessageDigest
                    .getInstance("MD5");
            md.update(source);
            byte tmp[] = md.digest(); // MD5 的计算结果是一个 128 位的长整数，
            // 用字节表示就是 16 个字节
            char str[] = new char[16 * 2]; // 每个字节用 16 进制表示的话，使用两个字符，
            // 所以表示成 16 进制需要 32 个字符
            int k = 0; // 表示转换结果中对应的字符位置
            for (int i = 0; i < 16; i++) { // 从第一个字节开始，对 MD5 的每一个字节
                // 转换成 16 进制字符的转换
                byte byte0 = tmp[i]; // 取第 i 个字节
                str[k++] = hexDigits[byte0 >>> 4 & 0xf]; // 取字节中高 4 位的数字转换,
                // >>> 为逻辑右移，将符号位一起右移
                str[k++] = hexDigits[byte0 & 0xf]; // 取字节中低 4 位的数字转换
            }
            s = new String(str); // 换后的结果转换为字符串

        } catch (Exception e) {
            e.printStackTrace();
        }
        return s;
    }


}
